package dex.compiler.interpreter;


import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.SortedSet;
import java.util.TreeSet;

import dex.compiler.model.definition.Unit;
import dex.test.TemplateSuite;

import junit.framework.TestCase;


public class InterpreterSuite extends TemplateSuite {


	public InterpreterSuite(Class cls, File dir) throws IOException {
		super(dir, expected(cls));
	}
	
	
	protected void doTest(Unit unit, String expected) {
		Object evaluated = new Interpreter(getProgram()).execute(unit.getName() + ":main");
		TestCase.assertEquals(expected, evaluated.toString());
		
	}

	
	private static SortedSet<String> expected(Class cls) {
		TreeSet<String> result = new TreeSet<String>();
		Method[] methods = cls.getDeclaredMethods();
		for (Method m: methods) {
			String n = getVisitorName(m);
			if (n != null) {
				result.add(n);
			}
		}
		return result;
	}

	
	private static String getVisitorName(Method m) {
		String name = m.getName();
		if ((name.startsWith("visit") || name.startsWith("enter")) && (name.length() > 5) && (m.getParameterTypes().length == 1)) {
			return name.substring(5).toUpperCase();
		}
		return null;
	}


}
